﻿var game = new Phaser.Game(640,1080,Phaser.CANVAS,'game');
var score = 0, time = 0, speeds =1000, game_bg = null, speedX = 10, speedY = 20, timeLimit = 30;
var gameBoot = {
	preload: function ( ){
		this.game.stage.backgroundColor = '#e6736c';
		this.input.maxPointers = 1;
		if(!game.device.desktop){
      		this.stage.forcePortrait = true;
      		this.scale.scaleMode = Phaser.ScaleManager.EXACT_FIT;
  		}else{
      		this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
  		}
  		this.scale.pageAlignHorizontally = true;
 		this.scale.pageAlignVertically = true;
  		this.scale.setScreenSize(true);
  		this.scale.refresh();
		game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
		game.scale.pageAlignHorizontally = true;
		game.load.image('logo', ROOT+'images/logo.png');
		game.load.image('game_bg', ROOT+'images/bg.png');
		game.load.image('sd', ROOT+'images/sd.png');
		game.load.image('gfchj', ROOT+'images/chj.png');
		game.load.image('gfpcdy', ROOT+'images/pcdy.png');
		game.load.image('gfmdy', ROOT+'images/mdy.png');
		game.load.image('gfjx', ROOT+'images/jx.png');
		game.load.image('boom', ROOT+'images/boom.png');
		game.load.image('over_bg', ROOT+'images/over.png');
		game.load.image('btn', ROOT+'images/btn.png');
		game.load.image('time_proc', ROOT+'images/timeline.png');//时间进度条
		game.load.image('sec', ROOT+'images/timeblock.png');//进度块
		game.load.image('btn2', ROOT+'images/btn2.png');
		game.load.image('gameload', ROOT+'images/gameload.png');
	},

	create: function( ){
		loading_pic = game.add.image(0,0,'loading_pic');
		loading_pic.anchor.set(0.5,0.5);
		loading_pic.inputEnabled = true;
		game.state.start('preload');
	},
}
var gamePreload ={
	create: function( ){
		game_go = game.add.image(0,0,'gameload');
		game_go.anchor.set(0,0);
		game_go.inputEnabled = true;
		game_go.events.onInputDown.addOnce(function(){
			game.state.start('start');
		});
	}
}
var gameStart = {
	create: function( ){
		game.physics.startSystem(Phaser.Physics.ARCADE);
		game_bg = game.add.tileSprite(0,0,game.width,game.height,'game_bg');
		this.sd_pic = game.add.sprite(450,25,'logo');
		this.sd_pic = game.add.sprite(game.world.centerX,game.world.bounds.bottom,'sd');
		this.sd_pic.anchor.setTo(0.5,1.0);
		this.sd_pic.inputEnabled = true;
		this.sd_pic.input.enableDrag();
		this.sd_pic.input.allowVerticalDrag = false;
		game.physics.arcade.enable(this.sd_pic);
		this.sd_pic.body.collideWorldBounds = true;

		game.add.image(25, 25, 'time_proc');//时间进度条
		this.timeCount = game.add.text(65, 62, '30', {
			font: '35px arial',
			fill: 'red'
		});
		this.timeCount.anchor.setTo(0.5,0.5);
		this.secBar = game.add.image(412, 77, 'sec');//添加进度块
		this.secBar.scale.setTo(0, 1);
		this.secBar.anchor.setTo(1,1);

		this.gfchj = game.add.group();
		this.gfchj.enableBody = true;
		this.gfchj.createMultiple(5, 'gfchj'); 
		this.timer = this.game.time.events.loop(950, this.addgoodnum, this, this.gfchj);

		this.gfpcdy = game.add.group();
		this.gfpcdy.enableBody = true;
		this.gfpcdy.createMultiple(5, 'gfpcdy'); 
		this.timer = this.game.time.events.loop(1000, this.addgoodnum, this, this.gfpcdy);

		this.gfmdy = game.add.group();
		this.gfmdy.enableBody = true;
		this.gfmdy.createMultiple(5, 'gfmdy'); 
		this.timer = this.game.time.events.loop(1100, this.addgoodnum, this, this.gfmdy);

		this.gfjx = game.add.group();
		this.gfjx.enableBody = true;
		this.gfjx.createMultiple(5, 'gfjx'); 
		this.timer = this.game.time.events.loop(800, this.addgoodnum, this, this.gfjx);

		this.boom = game.add.group();
		this.boom.enableBody = true;
		this.boom.createMultiple(10, 'boom'); 
		this.timer = this.game.time.events.loop(900, this.addgoodnum, this, this.boom);

		this.timer = game.time.events.loop(1000, this.ticktok, this);

	},
	update:function( ){
		game.physics.arcade.overlap(this.sd_pic, this.gfchj, this.add_score50, null, this);
		game.physics.arcade.overlap(this.sd_pic, this.gfpcdy, this.add_score30, null, this);
		game.physics.arcade.overlap(this.sd_pic, this.gfmdy, this.add_score10, null, this);
		game.physics.arcade.overlap(this.sd_pic, this.gfjx, this.add_score0, null, this);
		game.physics.arcade.overlap(this.sd_pic, this.boom, this.reduce_score10, null, this);
	},
	addGoodnum: function (x, y,obj) {
		var goodnum = obj.getFirstDead();
		goodnum.anchor.setTo(0.5,0.5);
		goodnum.reset(x, y);
		goodnum.body.gravity.y =  this.rnd.integerInRange(300, 500);
		goodnum.body.velocity.y =  this.rnd.integerInRange(speedX, speedY);
		goodnum.checkWorldBounds = true;
		goodnum.outOfBoundsKill = true;
	},
	addgoodnum: function (obj) {
		this.addGoodnum(this.rnd.integerInRange(120, 550), -100, obj);
	},
	add_score50: function (sd_pic,obj) {
		if (obj.exists) {
			obj.kill();
			score += 50;
			console.log(score);
		}
	},
	add_score30: function (sd_pic,obj) {
		if (obj.exists) {
			obj.kill();
			score += 30;
			console.log(score);
		}
	},
	add_score10: function (sd_pic,obj) {
		if (obj.exists) {
			obj.kill();
			score += 10;
		}
	},
	add_score0: function (sd_pic,obj) {
		if (obj.exists) {
			obj.kill();
			score += 0;
			console.log(score);
		}
	},
	reduce_score10: function (sd_pic,obj) {
		if (obj.exists) {
			obj.kill();
			score -= 10;
			console.log(score);
		}
	},
	// 倒计时
	ticktok: function () {
		timeLimit--;
		this.timeCount.text = timeLimit;
		this.secBar.scale.setTo(1-Math.round(timeLimit * 100 / 30)/100, 1);
		if (timeLimit === 0) {
			game.state.start('over');
		}
	}
}
var gameOver = {
	create: function( ){
		game_bg = game.add.tileSprite(0,0,game.width,game.height,'game_bg');
		this.sd_pic = game.add.sprite(450,25,'logo');
		this.sd_pic = game.add.sprite(game.world.centerX,game.world.bounds.bottom,'sd');
		this.sd_pic.anchor.setTo(0.5,1.0);
		game.add.image(25, 25, 'time_proc');
		this.timeCount = game.add.text(65, 62, '0', {
			font: '35px arial',
			fill: 'red'
		});
		this.timeCount.anchor.setTo(0.5,0.5);
		this.secBar = game.add.image(412, 77, 'sec');
		this.secBar.scale.setTo(1, 1);
		this.secBar.anchor.setTo(1,1);

		over_bg = game.add.image(game.world.centerX,game.world.centerY,'over_bg');
		over_bg.anchor.setTo(0.5,0.5);

		if(score == 0 ){
			total_score = game.add.text(game.width/2,game.height/2-10,'0',{font: '50px arial',fill: '#ffffff'});
		}
		else{
			total_score = game.add.text(game.width/2,game.height/2-10,score,{font: '50px arial',fill: '#ffffff'});
		}
		total_score.anchor.setTo(0.5,0.5);

		btn = game.add.image(game.width/2,game.height/2+80,'btn');
		btn.anchor.setTo(0.5,0.5);
		btn.inputEnabled = true;
		btn.events.onInputDown.addOnce(function(){
			location.href="index.php?g=mobile&m=index&a=index&type=rank";
		});

		btn2 = game.add.image(game.width/2,game.height/2+160,'btn2');
		btn2.anchor.setTo(0.5,0.5);
		btn2.inputEnabled = true;
		btn2.events.onInputDown.addOnce(function(){
			score = 0, time = 0, speeds =1000, game_bg = null, speedX = 10, speedY = 20, timeLimit = 3;
			game.state.start('start');
		});
		$.ajax({
			url: 'index.php?g=mobile&m=index&a=sendScore',
			type: 'POST',
			dataType: 'json',
			data: {
				wxid: wxid,
				openid: openid,
				jid: jid,
				shareWxid: shareWxid,
				shareOpenid: shareOpenid,
				sign: sign,
				gameType: gameType,
				score: score
			},
			success: function(res)
			{
				//alert(res.content);
			},
			error: function(res)
			{
				
			}
		});
	},
}

game.state.add('boot',gameBoot);
game.state.add('preload',gamePreload);
game.state.add('start',gameStart);
game.state.add('over',gameOver);
game.state.start('boot');
